
//Eli Elder
//village random test


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h> 
#include <assert.h>
#include <math.h>
#include "rngs.h"
#include <time.h>
//set PRINTOUT to 0 to remove printfs out
#define PRINTOUT 1
#define ASSERTIVE 0

int checkVillage(int village, int handPos, struct gameState *afterVillageState){
    struct gameState beforeVillageState;
    int returnVal;
    int actions;
    int player = whoseTurn(afterVillageState);
    //copy so we can test if the Village we can check
    memcpy(&beforeVillageState, afterVillageState, sizeof(struct gameState));
    
#if (PRINTOUT == 1)
    //check the number of actions
    actions = afterVillageState->numActions;
#endif
    
    //do the Village function card, choices, and bonus are unimportant hence 0's
    returnVal = cardEffect(village, 0, 0, 0, afterVillageState, handPos, 0 );

    //draw a card
    drawCard(player, &beforeVillageState);
    //increase the actions
    beforeVillageState.numActions = beforeVillageState.numActions + 2;
    //discard the village 
    discardCard(handPos, player, &beforeVillageState, 0);
    
    //I like the printout method more to see what the actual thing that happened is
#if (PRINTOUT == 1)
    //if the draw card is the same then that part worked well 
    if(beforeVillageState.hand[player][numHandCards(&beforeVillageState) - 1] == afterVillageState->hand[player][numHandCards(afterVillageState) - 1]){
	printf("The before village state and after village state have the same card drawn, which is good\n");
    } else {
	printf("The card drawn is different before %d : after %d\n", beforeVillageState.hand[player][numHandCards(&beforeVillageState) - 1], afterVillageState->hand[player][numHandCards(afterVillageState) - 1]);
    }

    
    //check the actions
    if(beforeVillageState.numActions == afterVillageState->numActions){
	printf("The number of actions is the same which is good\n");
    } else {
	printf("The number of actions are different before %d : after %d\n", beforeVillageState.numActions, afterVillageState->numActions);
    }

    if(actions != afterVillageState->numActions){
	printf("The number of actions done is different which is good\n");
    } else {
	printf("The number of actions are the same form before(bad) %d : after %d\n", actions, afterVillageState->numActions);
    }

    
    //check the top of the discard
    if(beforeVillageState.discard[player][beforeVillageState.discardCount[player] - 1] == afterVillageState->discard[player][afterVillageState->discardCount[player] - 1]){
	printf("The discard top card is the same card, good. \n\n");
    } else {
	printf("The discard card is not the same card, bad!\n\n");
    }
#endif
    
    //just in case we can do the assert method, though I don't like how it just dies
#if (ASSERTIVE == 1)
    //assert the draw card
    assert(beforeVillageState.hand[player][numHandCards(beforeVillageState) - 1] == afterVillageState->hand[player][numHandCards(afterVillageState) - 1]);
    
    //check the actions by asserting
    assert(beforeVillageState.numActions[player] == afterVillageState->numActions[player]);
    
    //check the top of the discard by assertomg
    assert(beforeVillageState.discard[player][beforeVillageState.discardCount[player] - 1] == afterVillageState->discard[player][afterVillageState->discardCount[player] - 1]);
    
    //asser tthe return value was 0, not too helpful for specifics but it helps
    assert(returnVal == 0);
#endif
    
    return 0;
}


int main(){
    int i;
    int ranTimes;
    int player;
    int handPos;
    struct gameState theGameState;
    //int kingCards[10] = {Village, council_room, feast, gardens, mine, remodel, smithy, village, baron, salvager};
   
    srand(time(0));
    for (ranTimes = 0; ranTimes < 20; ranTimes++) {
	for (i = 0; i < sizeof(struct gameState); i++) {
	    ((char*)&theGameState)[i] = floor(rand() % 256);
	}
	printf("Test number: %d\n", ranTimes);
	
	//random player
	player = floor(rand() % 4);
	theGameState.whoseTurn = player;
	
	//random deck/discard/hand
	theGameState.deckCount[player] = floor(rand() % MAX_DECK);
	theGameState.discardCount[player] = floor(rand() % MAX_DECK);
	theGameState.handCount[player] = floor(rand() % MAX_HAND);

	//fill up the deck, don't need specific cards in discard
	for(i = 0; i < theGameState.deckCount[player]; i++){
	    //set the deck to a specific card (only 27 cards so 0 to 27)
	    theGameState.deck[player][i] = floor(rand() % 27);
	}
	
	//fill up the hand, 
	for(i = 0; i < theGameState.handCount[player]; i++){
	    //set the deck to a specific card (only 27 cards so 0 to 27)
	    theGameState.hand[player][i] = floor(rand() % 27);
	}

	//fill up the discard 
	for(i = 0; i < theGameState.discardCount[player]; i++){
	    //set the deck to a specific card (only 27 cards so 0 to 27)
	    theGameState.discard[player][i] = floor(rand() % 27);
	}
	//but make sure to place a village somewhere 
	handPos = floor(rand() % theGameState.handCount[player]);
	theGameState.hand[player][handPos] = village;
	
	theGameState.playedCardCount = 0;
	theGameState.playedCards[theGameState.playedCardCount];
	//setup the actions
	theGameState.numActions = 1;

	//printf("handpos %d\n", handPos);

	checkVillage(village, handPos, &theGameState);
    }
 
    return 0;
}
